package com.liu.shop.controller;

import com.liu.shop.common.utils.R;
import com.liu.shop.common.vo.AddressVo;
import com.liu.shop.common.vo.OrderVo;
import com.liu.shop.common.vo.UserCarVo;
import com.liu.shop.common.vo.UserVo;
import com.liu.shop.entity.OrderEntity;
import com.liu.shop.feign.AddressService;
import com.liu.shop.feign.CarService;
import com.liu.shop.interceptor.LoginInterceptor;
import com.liu.shop.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.HashSet;
import java.util.List;
import java.util.concurrent.ExecutionException;

/**
 * 用户点击结算，跳转到结算页面。
 * 需要商品信息和地址信息，转发到订单页面
 */
@Controller
@RequestMapping("/order")
@Slf4j
public class OrderController {

    @Autowired
    private LoginInterceptor loginInterceptor;

    @Autowired
    private AddressService addressService;

    @Autowired
    private CarService carService;
    @Autowired
    private OrderService orderService;

    /**
     * 用户地址展示，订单商品信息展示
     * @param key
     * @param modelMap
     * @return
     */
    @RequestMapping("/toOrderPay")
    public String toOrderPay(@RequestParam("key") HashSet<Object> key,ModelMap modelMap){
        log.debug("接收的key为：{}",key);
        // 获取成功登录的用户信息
        UserVo userVo = loginInterceptor.get();

        // 获取用户的收获地址信息
        List<AddressVo> addressList = addressService.getAddressListByUid(Integer.parseInt(userVo.getId()));

        // 查询用户勾选的商品信息
        StringBuffer keyStr = new StringBuffer();
        Object[] objects = key.toArray();
        for (int i = 0; i < objects.length; i++) {
            if (i != (objects.length - 1)) {
                keyStr.append(objects[i]).append(",");
            } else {
                keyStr.append(objects[i]);
            }
        }
        // 获取用户在购物车中，勾选要准备购买的商品
        UserCarVo userCarVo = carService.getUserCar(keyStr.toString());

        // 4.把数组放到域中
        modelMap.put("addressList", addressList);
        modelMap.put("userCarVo", userCarVo);
        return "order";
    }

    @ResponseBody
    @RequestMapping("/orderCreate")
    public R orderCreate(@RequestBody OrderVo orderVo){
        String oid = null;
        try {
            oid = orderService.create(orderVo);
        } catch (ExecutionException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return R.ok().put("orderId",oid);
    }

    /**
     * 根据订单编号查询订单信息
     * @param orderId
     * @return
     */
    @GetMapping("/getOrderById/{orderId}")
    @ResponseBody
    public OrderEntity getOrderById(@PathVariable String orderId) {
        return orderService.getOrderById(orderId);
    }

    /**
     * 用户支付成功修改订单状态
     */
    @GetMapping("/updateOrderStatus")
    @ResponseBody
    void updateOrderStatus(@RequestParam("orderId") String orderId, @RequestParam("status") Integer status) {
        orderService.updateOrderStatus(orderId,status);
    }
}
